#include "MainHeader.h"


RHeader iHeader;
int ExportRoom(){
	int i=0;
    char FileName[1024]={0};
	char FCHK[] ="DHFH";//FileHeader
	FILE* fp=NULL;
	RHeader* RoomHeader=GameEngine->Members.RoomHeader;
	GBA.ReturnFileName("Please name your room\0*.dhr\0",FileName,512,0);
	if(FileName){
		fp = fopen(FileName,"w+");
		if(fp){
			iHeader= *RoomHeader;
			fseek(fp,0,SEEK_SET);
			fwrite(FCHK,strlen(FCHK),1,fp);
			if(iHeader.bBg0){
			   iHeader.lForeground=ftell(fp);
				fputc(ThisForeground->X,fp);
				fputc(ThisForeground->Y,fp);
				fwrite((u8*)ThisForeground->TileBuf2D,ThisForeground->X*ThisForeground->Y*2,1,fp);
			
			}else{
			  iHeader.lForeground=0xFFFFFF;

			}
			if(iHeader.bBg1){
				iHeader.lLevelData=ftell(fp);
				fputc(ThisLevelData->X,fp);
				fputc(ThisLevelData->Y,fp);
				fwrite((u8*)ThisLevelData->TileBuf2D,ThisLevelData->X*ThisLevelData->Y*2,1,fp);
			  
			}else{
				iHeader.lLevelData=0xFFFFFF;
			} 
			if(iHeader.bBg2){
				iHeader.lBackLayer=ftell(fp);  
				fputc(ThisBackLayer->X,fp);
				fputc(ThisBackLayer->Y,fp);
				fwrite((u8*)ThisBackLayer->TileBuf2D,ThisBackLayer->X*ThisBackLayer->Y*2,1,fp);
				
			}else{
				iHeader.lBackLayer=0xFFFFFF;
			} 
			if(iHeader.lBg3){
			    iHeader.lBackgroundTSA=ftell(fp);  
				fputc(ThisBackground->X,fp);
				fputc(ThisBackground->Y,fp);
				fwrite((u8*)ThisBackground->TileBuf2D,ThisBackground->X*ThisBackground->Y*2,1,fp);
			
			}else{
				iHeader.lBackgroundTSA=0xFFFFFF;
			} 
            iHeader.lSpritePointer=ftell(fp);
			for(i=0;i<Enemy1.max;i++){
            fputc(Enemy1.Enemies[i].Creature,fp);
			fputc(Enemy1.Enemies[i].X,fp);
            fputc(Enemy1.Enemies[i].Y,fp);

			}
			iHeader.lSpritePointer2=ftell(fp);
			for(i=0;i<Enemy2.max;i++){
            fputc(Enemy2.Enemies[i].Creature,fp);
			fputc(Enemy2.Enemies[i].X,fp);
            fputc(Enemy3.Enemies[i].Y,fp);

			}
			iHeader.lSpritePointer3=ftell(fp);
			for(i=0;i<Enemy3.max;i++){
            fputc(Enemy3.Enemies[i].Creature,fp);
			fputc(Enemy3.Enemies[i].X,fp);
            fputc(Enemy3.Enemies[i].Y,fp);

			}
			fwrite(&iHeader.bTileset,1,1,fp);
			fwrite(&iHeader.bBg0,1,1,fp);
			fwrite(&iHeader.bBg1,1,1,fp);
			fwrite(&iHeader.bBg2,1,1,fp);
			fwrite(&iHeader.lBg3,4,1,fp);
			fwrite(&iHeader.lForeground,4,1,fp); 
			fwrite(&iHeader.lLevelData,4,1,fp);
			fwrite(&iHeader.lBackLayer,4,1,fp);
			fwrite(&iHeader.lClipData,4,1,fp); 
			fwrite(&iHeader.lBackgroundTSA,4,1,fp);
			fwrite(&iHeader.bUnknown1,1,1,fp);
			fwrite(&iHeader.TransForeground,1,1,fp);
			fwrite(&iHeader.iSep1,2,1,fp);
			fwrite(&iHeader.lSpritePointer,4,1,fp); 
			fwrite(&iHeader.bSpriteIndex1,1,1,fp);
			fwrite(&iHeader.bEventSwitch,1,1,fp);
			fwrite(&iHeader.iSep2,2,1,fp);
			fwrite(&iHeader.lSpritePointer2,4,1,fp); 
			fwrite(&iHeader.bSpriteIndex2,1,1,fp);
			fwrite(&iHeader.bEventSwitch2,1,1,fp);
			fwrite(&iHeader.iSeperator,2,1,fp);
			fwrite(&iHeader.lSpritePointer3,4,1,fp); 
			fwrite(&iHeader.bSpriteIndex3,1,1,fp);
			fwrite(&iHeader.bMiniMapRoomX,1,1,fp);
			fwrite(&iHeader.bMiniMapRoomY,1,1,fp);
			fwrite(&iHeader.bEffect,1,1,fp);
			fwrite(&iHeader.bSceneryYPos,1,1,fp);
			fwrite(&iHeader.bNothing,1,1,fp);
			fwrite(&iHeader.bMusic,1,1,fp);
			fwrite(&iHeader.blank,1,1,fp);
            fclose(fp);
		} else {
			MessageBox(0,"Please enter a new file name","Error",MB_OK);
		}
	}
	return 0;
}
long MyEOF(FILE* fp){

    fseek(fp,0,SEEK_END);
	 return ftell(fp);
}

int ImportRoom(){

	return 0;
}
